package com.practice.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.practice.J2E.constant.NumGlobalConstant;
import com.practice.dto.user.ListUserParam;
import com.practice.entity.UserDO;
import com.practice.mapper.UserMapper;
import com.practice.response.ResponseL;
import com.practice.response.ResponseR;
import com.practice.service.UserService;
import com.practice.vo.user.DeleteUserParam;
import com.practice.vo.user.ListUserVO;
import com.practice.vo.user.UserBO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: kayn
 * 2024/5/8
 */
@Service
public class UserServiceImpl extends MPJBaseServiceImpl<UserMapper, UserDO> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public ResponseL<ListUserVO> listUser(ListUserParam param) {

        //查询条件
        MPJLambdaWrapper<UserDO> wrapper = JoinWrappers.lambda(UserDO.class)
                .selectAll(UserDO.class)
                .orderByDesc(UserDO::getCreateTime)
                .logicDelToOn();

        // 昵称模糊
        if (StrUtil.isNotBlank(param.getUserName())) {
            wrapper.like(UserDO::getUserName,param.getUserName());
        }

        // 用户名模糊
        if (StrUtil.isNotBlank(param.getLoginName())) {
            wrapper.like(UserDO::getLoginName,param.getLoginName());
        }

        // 手机号模糊
        if (StrUtil.isNotBlank(param.getPhone())) {
            wrapper.like(UserDO::getPhone,param.getPhone());
        }

        //分页参数
        Page<ListUserVO> page = new Page<>(param.getPageNo(), param.getPageSize());

        //查询
        Page<ListUserVO> userVOPage = this.selectJoinListPage(page, ListUserVO.class, wrapper);

        return ResponseL.successWithMessage("查询成功",userVOPage.getRecords(),userVOPage.getTotal());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseR<String> saveOrUpdateUser(UserBO userBO) {

        UserDO userDO = userBO.toDO();

        saveOrUpdate(userDO);

        return ResponseR.ok(userDO.getId());
    }

    @Override
    public Integer getUserNum() {

        int num;

        try {

            Long count = userMapper.selectCount(Wrappers.lambdaQuery(UserDO.class).eq(UserDO::getDeleted, Boolean.FALSE));

            num = count.intValue();

        }catch (Exception e) {
            num = NumGlobalConstant.ZERO;
        }

        return num;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseR<?> deleteUser(DeleteUserParam param) {

        boolean remove = this.removeById(param.getId());

        return remove ? ResponseR.okWithMsg("删除成功") : ResponseR.errorWithMsg("删除失败");
    }

    @Override
    public String getAvatar() {

        Integer userNum = getUserNum();
        if (userNum == 0) {
            return "no one";
        }

        int randomInt = RandomUtil.randomInt(userNum);

        UserDO userDO = this.getOne(Wrappers.lambdaQuery(UserDO.class).last("limit " + randomInt + ",1"));

        return userDO.getUserName();
    }
}
